#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "kernel_list.h"

// 大结构体
struct node
{
    int data;
    struct list_head list;
};

struct node *new_node(int data)
{
    struct node *new = calloc(1, sizeof(struct node));
    if(new != NULL)
        INIT_LIST_HEAD(&new->list);
    new->data = data;
    return new;
}

int main(int argc, char const *argv[])
{
    // 初始化一个空链表
    struct node *head = calloc(1, sizeof(struct node));
    if(head != NULL)
    {
        INIT_LIST_HEAD(&head->list);
        // head->list.prev = &head->list;
        // head->list.next = &head->list;
    }

    for(int i=1; i<5; i++)
    {
        struct node *new = new_node(i);
        list_add_tail(&new->list, &head->list);
    }

    // 遍历链表
    struct node *pos;
    list_for_each_entry(pos, &head->list, list)
    {
        printf("%d\t", pos->data);
    }
    printf("\n");

    // 销毁链表（head恢复初始状态）
    struct list_head *ptr, *n;
    list_for_each_safe(ptr, n, &head->list)
    {
        free(list_entry(ptr, struct node, list));
    }

    return 0;
}
